home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / Safe.Z / Safe
Encoding:
Text File  |  1998-10-28  |  14.6 KB  |  463 lines

  1.  
  2.  
  3.  
  4.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       Safe - Compile and execute code in restricted    compartments
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.         use    Safe;
  13.  
  14.         $compartment = new Safe;
  15.  
  16.         $compartment->permit(qw(time sort :browse));
  17.  
  18.         $result = $compartment->reval($unsafe_code);
  19.  
  20.  
  21.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  22.       The Safe extension module allows the creation    of
  23.       compartments in which    perl code can be evaluated. Each
  24.       compartment has
  25.  
  26.       a new    namespace
  27.           The "root" of    the namespace (i.e. "main::") is
  28.           changed to a different package and code evaluated in
  29.           the compartment cannot refer to variables outside
  30.           this namespace, even with run-time glob lookups and
  31.           other    tricks.
  32.  
  33.           Code which is    compiled outside the compartment can
  34.           choose to place variables into (or _s_h_a_r_e variables
  35.           with)    the compartment's namespace and    only that data
  36.           will be visible to code evaluated in the
  37.           compartment.
  38.  
  39.           By default, the only variables shared    with
  40.           compartments are the "underscore" variables $_ and
  41.           @_ (and, technically,    the less frequently used %_,
  42.           the _    filehandle and so on). This is because
  43.           otherwise perl operators which default to $_ will
  44.           not work and neither will the    assignment of
  45.           arguments to @_ on subroutine    entry.
  46.  
  47.       an operator mask
  48.           Each compartment has an associated "operator mask".
  49.           Recall that perl code    is compiled into an internal
  50.           format before    execution.  Evaluating perl code (e.g.
  51.           via "eval" or    "do 'file'") causes the    code to    be
  52.           compiled into    an internal format and then, provided
  53.           there    was no error in    the compilation, executed.
  54.           Code evaulated in a compartment compiles subject to
  55.           the compartment's operator mask. Attempting to
  56.           evaulate code    in a compartment which contains    a
  57.           masked operator will cause the compilation to    fail
  58.           with an error. The code will not be executed.
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  71.  
  72.  
  73.  
  74.           The default operator mask for    a newly    created
  75.           compartment is the ':default'    optag.
  76.  
  77.           It is    important that you read    the _O_p_c_o_d_e(3) module
  78.           documentation    for more information, especially for
  79.           detailed definitions of opnames, optags and opsets.
  80.  
  81.           Since    it is only at the compilation stage that the
  82.           operator mask    applies, controlled access to
  83.           potentially unsafe operations    can be achieved    by
  84.           having a handle to a wrapper subroutine (written
  85.           outside the compartment) placed into the
  86.           compartment. For example,
  87.  
  88.               $cpt = new Safe;
  89.               sub wrapper {
  90.               # vet    arguments and perform potentially unsafe operations
  91.               }
  92.               $cpt->share('&wrapper');
  93.  
  94.  
  95.      WWWWAAAARRRRNNNNIIIINNNNGGGG
  96.       The authors make nnnnoooo wwwwaaaarrrrrrrraaaannnnttttyyyy,    implied    or otherwise, about
  97.       the suitability of this software for safety or security
  98.       purposes.
  99.  
  100.       The authors shall not    in any case be liable for special,
  101.       incidental, consequential, indirect or other similar damages
  102.       arising from the use of this software.
  103.  
  104.       Your mileage will vary. If in    any doubt ddddoooo nnnnooootttt uuuusssseeee iiiitttt.
  105.  
  106.       RRRREEEECCCCEEEENNNNTTTT CCCCHHHHAAAANNNNGGGGEEEESSSS
  107.  
  108.       The interface    to the Safe module has changed quite
  109.       dramatically since version 1 (as supplied with Perl5.002).
  110.       Study    these pages carefully if you have code written to use
  111.       Safe version 1 because you will need to makes    changes.
  112.  
  113.       MMMMeeeetttthhhhooooddddssss iiiinnnn ccccllllaaaassssssss SSSSaaaaffffeeee
  114.  
  115.       To create a new compartment, use
  116.  
  117.           $cpt = new Safe;
  118.  
  119.       Optional argument is (NAMESPACE), where NAMESPACE is the
  120.       root namespace to use    for the    compartment (defaults to
  121.       "Safe::Root0", incremented for each new compartment).
  122.  
  123.       Note that version 1.00 of the    Safe module supported a    second
  124.       optional parameter, MASK.  That functionality    has been
  125.       withdrawn pending deeper consideration. Use the permit and
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  137.  
  138.  
  139.  
  140.       deny methods described below.
  141.  
  142.       The following    methods    can then be used on the    compartment
  143.       object returned by the above constructor. The    object
  144.       argument is implicit in each case.
  145.  
  146.       permit (OP, ...)
  147.           Permit the listed operators to be used when
  148.           compiling code in the    compartment (in    _a_d_d_i_t_i_o_n to
  149.           any operators    already    permitted).
  150.  
  151.       permit_only (OP, ...)
  152.           Permit _o_n_l_y the listed operators to be used when
  153.           compiling code in the    compartment (_n_o    other
  154.           operators are    permitted).
  155.  
  156.       deny (OP, ...)
  157.           Deny the listed operators from being used when
  158.           compiling code in the    compartment (other operators
  159.           may still be permitted).
  160.  
  161.       deny_only (OP, ...)
  162.           Deny _o_n_l_y the    listed operators from being used when
  163.           compiling code in the    compartment (_a_l_l other
  164.           operators will be permitted).
  165.  
  166.       trap (OP, ...)
  167.  
  168.       untrap (OP, ...)
  169.           The trap and untrap methods are synonyms for deny
  170.           and permit respectfully.
  171.  
  172.       share    (NAME, ...)
  173.           This shares the _v_a_r_i_a_b_l_e(s) in the argument list
  174.           with the compartment.     This is almost    identical to
  175.           exporting variables using the    the _E_x_p_o_r_t_e_r(_3)
  176.           manpage module.
  177.  
  178.           Each NAME must be the    nnnnaaaammmmeeee of    a variable, typically
  179.           with the leading type    identifier included. A
  180.           bareword is treated as a function name.
  181.  
  182.           Examples of legal names are '$foo' for a scalar,
  183.           '@foo' for an    array, '%foo' for a hash, '&foo' or
  184.           'foo'    for a subroutine and '*foo' for    a glob (i.e.
  185.           all symbol table entries associated with "foo",
  186.           including scalar, array, hash, sub and filehandle).
  187.  
  188.           Each NAME is assumed to be in    the calling package.
  189.           See share_from for an    alternative method (which
  190.           share    uses).
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  203.  
  204.  
  205.  
  206.       share_from (PACKAGE, ARRAYREF)
  207.           This method is similar to _s_h_a_r_e() but    allows you to
  208.           explicitly name the package that symbols should be
  209.           shared from. The symbol names    (including type
  210.           characters) are supplied as an array reference.
  211.  
  212.               $safe->share_from('main',    [ '$foo', '%bar', 'func' ]);
  213.  
  214.  
  215.       varglob (VARNAME)
  216.           This returns a glob reference    for the    symbol table
  217.           entry    of VARNAME in the package of the compartment.
  218.           VARNAME must be the nnnnaaaammmmeeee of a    variable without any
  219.           leading type marker. For example,
  220.  
  221.               $cpt = new Safe 'Root';
  222.               $Root::foo = "Hello world";
  223.               #    Equivalent version which doesn't need to know $cpt's package name:
  224.               ${$cpt->varglob('foo')} =    "Hello world";
  225.  
  226.  
  227.       reval    (STRING)
  228.           This evaluates STRING    as perl    code inside the
  229.           compartment.
  230.  
  231.           The code can only see    the compartment's namespace
  232.           (as returned by the rrrrooooooootttt method). The    compartment's
  233.           root package appears to be the main::    package    to the
  234.           code inside the compartment.
  235.  
  236.           Any attempt by the code in STRING to use an operator
  237.           which    is not permitted by the    compartment will cause
  238.           an error (at run-time    of the main program but    at
  239.           compile-time for the code in STRING).     The error is
  240.           of the form "%s trapped by operation mask
  241.           operation...".
  242.  
  243.           If an    operation is trapped in    this way, then the
  244.           code in STRING will not be executed. If such a
  245.           trapped operation occurs or any other    compile-time
  246.           or return error, then    $@ is set to the error
  247.           message, just    as with    an _e_v_a_l().
  248.  
  249.           If there is no error,    then the method    returns    the
  250.           value    of the last expression evaluated, or a return
  251.           statement may    be used, just as with subroutines and
  252.           eeeevvvvaaaallll(((()))). The context (list or scalar) is determined
  253.           by the caller    as usual.
  254.  
  255.           This behaviour differs from the beta distribution of
  256.           the Safe extension where earlier versions of perl
  257.           made it hard to mimic    the return behaviour of    the
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  269.  
  270.  
  271.  
  272.           _e_v_a_l() command and the context was always scalar.
  273.  
  274.           Some points to note:
  275.  
  276.           If the entereval op is permitted then    the code can
  277.           use eval "..." to 'hide' code    which might use    denied
  278.           ops. This is not a major problem since when the code
  279.           tries    to execute the eval it will fail because the
  280.           opmask is still in effect. However this technique
  281.           would    allow clever, and possibly harmful, code to
  282.           'probe' the boundaries of what is possible.
  283.  
  284.           Any string eval which    is executed by code executing
  285.           in a compartment, or by code called from code
  286.           executing in a compartment, will be eval'd in    the
  287.           namespace of the compartment.    This is    potentially a
  288.           serious problem.
  289.  
  290.           Consider a function _f_o_o() in package pkg compiled
  291.           outside a compartment    but shared with    it. Assume the
  292.           compartment has a root package called    'Root'.    If
  293.           _f_o_o()    contains an eval statement like    eval '$foo =
  294.           1' then, normally, $pkg::foo will be set to 1.  If
  295.           _f_o_o()    is called from the compartment (by whatever
  296.           means) then instead of setting $pkg::foo, the    eval
  297.           will actually    set $Root::pkg::foo.
  298.  
  299.           This can easily be demonstrated by using a module,
  300.           such as the Socket module, which uses    eval "..." as
  301.           part of an AUTOLOAD function.    You can    'use' the
  302.           module outside the compartment and share an
  303.           (autoloaded) function    with the compartment. If an
  304.           autoload is triggered    by code    in the compartment, or
  305.           by any code anywhere that is called by any means
  306.           from the compartment,    then the eval in the Socket
  307.           module's AUTOLOAD function happens in    the namespace
  308.           of the compartment. Any variables created or used by
  309.           the eval'd code are now under    the control of the
  310.           code in the compartment.
  311.  
  312.           A similar effect applies to _a_l_l runtime symbol
  313.           lookups in code called from a    compartment but    not
  314.           compiled within it.
  315.  
  316.       rdo (FILENAME)
  317.           This evaluates the contents of file FILENAME inside
  318.           the compartment.  See    above documentation on the
  319.           rrrreeeevvvvaaaallll    method for further details.
  320.  
  321.       root (NAMESPACE)
  322.           This method returns the name of the package that is
  323.           the root of the compartment's    namespace.
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  335.  
  336.  
  337.  
  338.           Note that this behaviour differs from    version    1.00
  339.           of the Safe module where the root module could be
  340.           used to change the namespace.    That functionality has
  341.           been withdrawn pending deeper    consideration.
  342.  
  343.       mask (MASK)
  344.           This is a get-or-set method for the compartment's
  345.           operator mask.
  346.  
  347.           With no MASK argument    present, it returns the
  348.           current operator mask    of the compartment.
  349.  
  350.           With the MASK    argument present, it sets the operator
  351.           mask for the compartment (equivalent to calling the
  352.           deny_only method).
  353.  
  354.       SSSSoooommmmeeee SSSSaaaaffffeeeettttyyyy IIIIssssssssuuuueeeessss
  355.  
  356.       This section is currently just an outline of some of the
  357.       things code in a compartment might do    (intentionally or
  358.       unintentionally) which can have an effect outside the
  359.       compartment.
  360.  
  361.       Memory  Consuming all    (or nearly all)    available memory.
  362.  
  363.       CPU      Causing infinite loops etc.
  364.  
  365.       Snooping
  366.           Copying private information out of your system. Even
  367.           something as simple as your user name    is of value to
  368.           others. Much useful information could    be gleaned
  369.           from your environment    variables for example.
  370.  
  371.       Signals Causing signals (especially SIGFPE and SIGALARM) to
  372.           affect your process.
  373.  
  374.           Setting up a signal handler will need    to be
  375.           carefully considered and controlled.    What mask is
  376.           in effect when a signal handler gets called?    If a
  377.           user can get an imported function to get an
  378.           exception and    call the user's    signal handler,    does
  379.           that user's restricted mask get re-instated before
  380.           the handler is called?  Does an imported handler get
  381.           called with its original mask    or the user's one?
  382.  
  383.       State    Changes
  384.           Ops such as chdir obviously effect the process as a
  385.           whole    and not    just the code in the compartment. Ops
  386.           such as rand and srand have a    similar    but more
  387.           subtle effect.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      SSSSaaaaffffeeee((((3333))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))           SSSSaaaaffffeeee((((3333))))
  401.  
  402.  
  403.  
  404.       AAAAUUUUTTTTHHHHOOOORRRR
  405.  
  406.       Originally designed and implemented by Malcolm Beattie,
  407.       mbeattie@sable.ox.ac.uk.
  408.  
  409.       Reworked to use the Opcode module and    other changes added by
  410.       Tim Bunce <_T_i_m._B_u_n_c_e@_i_g._c_o._u_k>.
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.